﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Xpom_Xpum__Browser_Integrator.Subsystems
{
    internal class CrxManager
    {
        internal void PackDirectoryToCRX(string browserPath, string directoryPath)
        {
            PackDirectoryToCRXWithPEM(browserPath, directoryPath, "");
        }

        internal void PackDirectoryToCRXWithPEM(string browserPath, string directoryPath,
            string PEMPath)
        {
            string arguments = "";
            arguments += "--pack-extension=\"{0}\""; // TODO

            if (!String.IsNullOrEmpty(PEMPath))
            {
                arguments += " --pack-extension-key=\"{1}\"";

                arguments = String.Format(arguments, directoryPath, PEMPath);
            }
            else
            {
                arguments = String.Format(arguments, directoryPath);
            }

            Console.WriteLine(arguments);
            arguments = arguments.TrimEnd(new char[] { '"' });
            Console.WriteLine(arguments);

            ProcessStartInfo browserStartInfo = new ProcessStartInfo();
            browserStartInfo.FileName = browserPath;
            browserStartInfo.Arguments = arguments;

            Process browserProcess = new Process();
            browserProcess.StartInfo = browserStartInfo;
            browserProcess.Start();
        }
    }
}
